package com.yanqu.road.server.manager.union.service;

import com.google.protobuf.AbstractMessage;
import com.google.protobuf.InvalidProtocolBufferException;
import com.yanqu.road.entity.union.cross.CrossUnionMember;
import com.yanqu.road.pb.syncMessage.SyncMessage;
import com.yanqu.road.pb.worldmessage.WorldMessageProto;
import com.yanqu.road.server.command.ICrossServiceCommand;
import com.yanqu.road.server.manager.player.WorldMessageMgr;
import com.yanqu.road.server.manager.union.CrossUnionMgr;
import com.yanqu.road.utils.string.StringUtils;

import java.util.List;
import java.util.concurrent.CompletableFuture;


public class GetUnionMemberChatListByUserIdCmdService implements ICrossServiceCommand {
    @Override
    public CompletableFuture<AbstractMessage.Builder<?>> execute(SyncMessage.Request request) throws InvalidProtocolBufferException {
        CompletableFuture<AbstractMessage.Builder<?>> promise = new CompletableFuture<>();
        long userId = request.getSenderId();
        WorldMessageProto.WorldMessageListMsg.Builder messageList = WorldMessageProto.WorldMessageListMsg.newBuilder();
        CrossUnionMember member = CrossUnionMgr.getUnionMemberByUserId(userId);
        if (member != null && !StringUtils.isNullOrEmpty(member.getUnionUid())) {
            List<WorldMessageProto.WorldMessageTempMsg.Builder> unionMessageList = WorldMessageMgr.getUnionMessageMsgList(member.getUnionUid());
            if (null != unionMessageList) {
                for (WorldMessageProto.WorldMessageTempMsg.Builder builder : unionMessageList) {
                    if (builder.getUserId() != userId) {
                        continue;
                    }
                    if (builder.getContentType() == 2) {
                        continue;
                    }
                    messageList.addDataList(builder);
                }
            }
        }
        promise.complete(messageList);
        return promise;
    }
}
